#include "homepage.h"
#include "ui_homepage.h"
#include <qlabel.h>
#include <qstring.h>
#include <QPixmap>
#include <qtimer.h>
#include <qpainter.h>
#include <QPen>
#include <qdatetime.h>
#include <qtabwidget.h>
#include <QPushButton>
#include <qfile.h>
#include <qregularexpression.h>
#include <vector>
#include <string>
using namespace std;

HomePage::HomePage(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::HomePage)
{
    ui->setupUi(this);
    ui->tabWidget->installEventFilter(this);
    this->setWindowTitle("地铁售票与乘车引导系统");
    this->setWindowIcon(QIcon(":Icon/image/BeijingSubwaylogo.png"));
    this->setMinimumSize(1024, 870);
    this->setMaximumSize(1024, 870);
    //this->resize(1000, 800);
    route = new Edge;
    edgeNum = 0;
    transNum = 0;
    length = 0;
    mapview = new Mapview;
    ticketP = new Tickettable;

    ui->centralwidget->layout()->setAlignment(Qt::AlignHCenter|Qt::AlignBottom);
    ui->widget_8->layout()->setAlignment(Qt::AlignBottom);

    ui->tabWidget->setCurrentIndex(0); //设置默认tab页
    ui->pushButton_6->setVisible(false);

    connect(ui->comboBox, &QComboBox::activated, ui->comboBox_2, [=]() {
        QString str = ui->comboBox->currentText();
        QFile station(":files/files/" + str + ".txt");
        station.open(QIODevice::ReadOnly);
        ui->comboBox_2->clear();
        while (!station.atEnd()) {
            QByteArray array2 = station.readLine();
            QString str2(array2);
            str2 = str2.trimmed();
            ui->comboBox_2->addItem(str2);
        }
        station.close();
        });

    connect(ui->tabWidget, &QTabWidget::currentChanged, [=]() {
        if (ui->tabWidget->currentIndex() == 0) {
            mapview->setRoute(ui->widget_12->getEdge(), ui->widget_12->getEdgeNum());
        }
        else if (ui->tabWidget->currentIndex() == 1) {
            mapview->setRoute(ui->widget_13->getEdge(), ui->widget_13->getEdgeNum());
        }
        });

    connect(ui->comboBox_3, &QComboBox::activated, ui->comboBox_4, [=]() {
        QString str = ui->comboBox_3->currentText();
        QFile station(":files/files/" + str + ".txt");
        station.open(QIODevice::ReadOnly);
        ui->comboBox_4->clear();
        while (!station.atEnd()) {
            QByteArray array2 = station.readLine();
            QString str2(array2);
            str2 = str2.trimmed();
            ui->comboBox_4->addItem(str2);
        }
        station.close();
        });

    QFile lines(":files/files/Lines.txt");   //读取线路信息到combobox
    lines.open(QIODevice::ReadOnly);

    while (!lines.atEnd()) {
        QByteArray array = lines.readLine();
        QString str(array);
        str = str.trimmed();
        ui->comboBox->addItem(str);
        ui->comboBox_3->addItem(str);
    }
    lines.close();

    QString str = ui->comboBox->currentText();
    QFile station(":files/files/" + str + ".txt");

    station.open(QIODevice::ReadOnly);

    while (!station.atEnd()) {
        QByteArray array2 = station.readLine();
        QString str2(array2);
        str2 = str2.trimmed();
        ui->comboBox_2->addItem(str2);
        ui->comboBox_4->addItem(str2);
    }
    station.close();

    QFile sights(":/files/files/spots.txt");
    sights.open(QIODevice::ReadOnly);
    QTextStream travel(&sights);
    while (true) {
        QString spots = travel.readLine();
        spots = spots.trimmed();
        if (spots.isNull()) {
            break;
        }
        QStringList parts = spots.split(",");     //获取景点和相应地铁站，前面是景点，后面是地铁站
        ui->comboBox_5->addItem(parts.at(0));
    }

    QLabel* label = new QLabel(QString::fromStdString("欢迎使用北京地铁售票与乘车引导系统"), this);
    ui->statusBar->addWidget(label);

    QTimer* timer = new QTimer(this);  //状态栏右下角显示当前时间
    timer->start(1000); // 每次触发timeout信号时间间隔为1秒
    connect(timer, SIGNAL(timeout()), this, SLOT(TimeUpdate()));
    timeLabel = new QLabel(this);

    connect(ui->pushButton, &QPushButton::clicked, this, [=]() {
        Graph g;
        Dist* D;
        delete[] route;
        string start = ui->comboBox_2->currentText().toStdString();
        string end="";
        int footmiles = 0;
        if (ui->tabWidget->currentIndex() == 0) {
            end = ui->comboBox_4->currentText().toStdString();
        }
        else {
            QFile sight(":/files/files/spots.txt");
            sight.open(QIODevice::ReadOnly);
            QTextStream traveling(&sight);
            while (true) {
                QString spots = traveling.readLine();
                spots = spots.trimmed();
                if (spots.isNull()) {
                    break;
                }
                QStringList parts = spots.split(",");     //获取景点和相应地铁站，前面是景点，后面是地铁站
                if (parts.at(0) == ui->comboBox_5->currentText()) {
                    end = parts.at(1).toStdString();
                    footmiles = parts.at(2).toInt();
                }
            }
        }
        g.Dijkstra(g.getStationNO(start), D);
        route = g.Route(g.getStationNO(start), g.getStationNO(end), D);
        edgeNum = g.getEdgeNum(g.getStationNO(start), g.getStationNO(end), D);
        length = g.Length(g.getStationNO(start), g.getStationNO(end), D);
        transNum = g.TranNum(g.getStationNO(start), g.getStationNO(end), D);
        
        if (ui->tabWidget->currentIndex() == 0) {
            ui->widget_12->setPaintMode(1);
            ui->widget_12->setAttr(route, edgeNum, transNum, length, start);
        }
        else {
            if (ui->widget_13->getPaintMode() == 0 || ui->widget_13->getPaintMode() == 1) {
                ui->widget_13->setPaintMode(1);
                ui->widget_13->setAttr(route, edgeNum, transNum, length, footmiles, start, ui->comboBox_5->currentText().toStdString());
            }
            else if (ui->widget_13->getPaintMode() == 2) {
                ui->widget_13->setAttr(route, edgeNum, transNum, length, footmiles, start, ui->comboBox_5->currentText().toStdString());
            }
        }

        if (ui->widget_13->getPaintMode() == 2)  //多景点规划状态
        {
            ui->comboBox->setCurrentText("All");
            ui->comboBox_2->setCurrentText(QString::fromStdString(end));
            ui->comboBox->setEnabled(false);
            ui->comboBox_2->setEnabled(false);
        }

        if (ui->tabWidget->currentIndex() == 0) {
            mapview->setRoute(route, edgeNum);  //更新总线窗口
        }
        else if (ui->tabWidget->currentIndex() == 1) {
            mapview->setRoute(ui->widget_13->getEdge(), ui->widget_13->getEdgeNum());
        }

        update();
        });

    connect(ui->pushButton_2, &QPushButton::clicked, this, [=]() {
        Graph g;
        Dist* D;
        delete[] route;
        int footmiles = 0;
        string start = ui->comboBox_2->currentText().toStdString();
        string end = "";
        if (ui->tabWidget->currentIndex() == 0) {
            end = ui->comboBox_4->currentText().toStdString();
        }
        else {
            QFile sight(":/files/files/spots.txt");
            sight.open(QIODevice::ReadOnly);
            QTextStream traveling(&sight);
            while (true) {
                QString spots = traveling.readLine();
                spots = spots.trimmed();
                if (spots.isNull()) {
                    break;
                }
                QStringList parts = spots.split(",");     //获取景点和相应地铁站，前面是景点，后面是地铁站
                if (parts.at(0) == ui->comboBox_5->currentText()) {
                    end = parts.at(1).toStdString();
                    footmiles = parts.at(2).toInt();
                }
            }
        }
        g.Dijk_minTrans(g.getStationNO(start), D);
        route = g.Route(g.getStationNO(start), g.getStationNO(end), D);
        edgeNum = g.getEdgeNum(g.getStationNO(start), g.getStationNO(end), D);
        length = g.Length(g.getStationNO(start), g.getStationNO(end), D);
        transNum = g.TranNum(g.getStationNO(start), g.getStationNO(end), D);
        
        if (ui->tabWidget->currentIndex() == 0) {
            ui->widget_12->setPaintMode(1);
            ui->widget_12->setAttr(route, edgeNum, transNum, length, start);
        }
        else {
            if (ui->widget_13->getPaintMode() == 0 || ui->widget_13->getPaintMode() == 1) {
                ui->widget_13->setPaintMode(1);
                ui->widget_13->setAttr(route, edgeNum, transNum, length, footmiles, start, ui->comboBox_5->currentText().toStdString());
            }
            else if(ui->widget_13->getPaintMode() == 2) {
                ui->widget_13->setAttr(route, edgeNum, transNum, length, footmiles, start, ui->comboBox_5->currentText().toStdString());
            }
        }

        if (ui->widget_13->getPaintMode() == 2)  //多景点规划状态
        {
            ui->comboBox->setCurrentText("All");
            ui->comboBox_2->setCurrentText(QString::fromStdString(end));
            ui->comboBox->setEnabled(false);
            ui->comboBox_2->setEnabled(false);
        }

        if (ui->tabWidget->currentIndex() == 0) {
            mapview->setRoute(route, edgeNum);  //更新总线窗口
        }
        else if (ui->tabWidget->currentIndex() == 1) {
            mapview->setRoute(ui->widget_13->getEdge(), ui->widget_13->getEdgeNum());
        }
        update();
        });

    static bool flag = false;  //标记，1->第二次点击
    connect(ui->pushButton_5, &QPushButton::clicked, this, [=]() {
        if (flag == false) {
            ui->widget_13->setPaintMode(2);
            ui->pushButton_5->setText("结束多景点规划");
            ui->widget_13->clearEdge();
            mapview->clearRoute();
            ui->widget_13->clearAccuCost();  //清除累积价格
            ui->widget_13->setSpotNum(0);  //游览景点数清零
            ui->widget_13->clearLength();   //将路程清0
            ui->widget_13->clearTrans(); //清除换乘数
            update();
        }
        else {
            ui->widget_13->setPaintMode(1);
            ui->pushButton_5->setText("开始多景点规划");
            ui->widget_13->setSpotNum(1);  //游览景点数复位，默认为1
            ui->widget_13->clearAccuCost();  //清除累积价格
            ui->widget_13->clearEdge();
            ui->widget_13->clearTrans(); //清除换乘数
            ui->comboBox->setEnabled(true);
            ui->comboBox_2->setEnabled(true);
            update();
        }
        flag = !flag;
        });

    connect(ui->pushButton_4, &QPushButton::clicked, this, [=]() {
        mapview->setRoute(route,edgeNum);
        mapview->show();
        });

    connect(ui->pushButton_3, &QPushButton::clicked, this, [=]() {
        ticketP->show();
        });

    this->installEventFilter(this);  //安装事件过滤器
}


void HomePage::TimeUpdate()
{
    QDateTime CurrentTime = QDateTime::currentDateTime();
    QString strTime = CurrentTime.toString(" yyyy年MM月dd日 hh:mm:ss "); //设置显示的格式
    timeLabel->setText(strTime);
    statusBar()->addPermanentWidget(timeLabel);
    statusBar()->setSizeGripEnabled(true); //设置是否显示右边的大小控制点

}

void HomePage::resizeEvent(QResizeEvent* event)
{
    ui->tabWidget->tabBar()->setFixedWidth(ui->tabWidget->width());
}

bool HomePage::eventFilter(QObject* obj, QEvent* event)
{
    if (obj == ui->tabWidget)
    {
        if (event->type() == QEvent::Resize) {
            ui->tabWidget->tabBar()->setFixedWidth(ui->tabWidget->width());
            return true;
        }
        return true;
    }
    else if (obj == this) {/*判断发生事件的是否为主窗口*/
            /*判断事件的类型是否为关闭事件*/
            if (event->type() == QEvent::Close) {
                qApp->quit();//主窗口关闭事件,程序退出,其他窗口将被关闭
                return true; //返回true代表事件已被处理不需要向下传递，返回false代表事件未被处理需要向下传递由别的组件处理
            }
    }

    /*不是我们关心的事件，交由QMainWindow的事件过滤器处理*/
    return QMainWindow::eventFilter(obj, event);
}

void HomePage::setRoute(Edge* r) {
    route = r;
}

HomePage::~HomePage()
{
    delete ui;
    delete[] route;
    delete mapview;
    delete ticketP;
}